<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class TmpDocService extends BaseService
{
    //模板接口文档

    /**
     * 列表
     * @param int $item_id 项目ID
     * @param int $client_type 用户端类型：1前台，2后台
     */
    public function getIndex($item_id, $client_type = 0)
    {
        $db = $this->dbConn($this->dbTmpDoc)
            ->where('del', 0);
        if ($item_id) {
            $db = $db->where('item_id', $item_id);
        }
        if ($client_type) {
            $db = $db->where('client_type', $client_type);
        }
        $res = $db->orderBy('id', 'asc')->get();
        if (!count($res)) {
            return $this->toback(0, '没有项目', []);
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'item_id' => $v['item_id'],
                'client_type' => $v['client_type'],
                'title' => $v['title'],
                'url' => $v['url'],
                'req_json' => $v['req_json'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //详情
    public function getOne($docId)
    {
        $res = (array) $this->dbConn($this->dbTmpDoc)->where('id', $docId)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有接口');
        }
        $arys = [
            'id' => $res['id'],
            //'item_id' => $res['item_id'],
            //'client_type' => $res['client_type'],
            'title' => $res['title'],
            'url' => env('DOMAIN') . $res['url'],
            'req_json' => $res['req_json'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param array $pas【item_id项目ID，client_type用户端类型，title接口名，url地址，req_json请求参数】
     * */
    public function add($pas)
    {
        //看看有没有
        $resOne = (array) $this->dbConn($this->dbTmpDoc)
            ->where('item_id', $pas['item_id'])
            ->where('client_type', $pas['client_type'])
            ->where('title', $pas['title'])
            ->first();
        if (!empty($resOne)) {
            return $this->toback(-2, '接口已存在');
        }
        //保存
        $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbTmpDoc)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id 接口ID
     * @param array $pas【title接口名，url地址，req_json请求参数】
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbTmpDoc)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有接口');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbTmpDoc)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    //删除
    public function del($id)
    {
        $resOne = $this->getOne($id);
        if (!$resOne) {
            return $this->toback(-1, '没有该接口');
        }
        $up = $this->dbConn($this->dbTmpDoc)->where('id', $id)
            ->update([
                'del' => 1,
                'deltime' => toDate(),
            ]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}